package black.market.weather;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.app.AlarmManager;
import android.content.Context;
import android.text.format.DateFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import black.market.main.R;
import black.market.util.Log;

public class WeatherExpandableListAdapter extends BaseExpandableListAdapter {
	public static final String TAG = "WeatherExpandableListAdapter";
	private String[] groups = null;;
	private ArrayList<KmaInfoBean> todayList = null;
	private ArrayList<KmaInfoBean> tomorrowList = null;
	private ArrayList<KmaInfoBean> afterTomorrowList = null;
	private LayoutInflater mLayoutInflater = null;;
	private Context mContext = null;
	private int lastExpandedGroupPosition = 0;

	public WeatherExpandableListAdapter(Context context, KmaInfoDAO dao) {
		mContext = context;
		mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		Calendar c = Calendar.getInstance();
		long timeMap = dao.getKmaHeaderLastTimeMapLong();
		Log.d(TAG, new Date(timeMap).toString());
		groups = new String[3];
		groups[0] = DateFormat.format("yyyy" + "년 " + DateFormat.MONTH + "월 " + DateFormat.DATE + "일", timeMap).toString();
		timeMap = dao.getKmaHeaderLastTimeMapLong() + AlarmManager.INTERVAL_DAY;
		groups[1] = DateFormat.format("yyyy" + "년 " + DateFormat.MONTH + "월 " + DateFormat.DATE + "일", timeMap).toString();
		timeMap = dao.getKmaHeaderLastTimeMapLong() + AlarmManager.INTERVAL_DAY * 2;
		groups[2] = DateFormat.format("yyyy" + "년 " + DateFormat.MONTH + "월 " + DateFormat.DATE + "일", timeMap).toString();

		// Log.d(TAG, "WeatherExpandableListAdapter date -> " +
		// date.toLocaleString());
		// c.setTimeInMillis(date.getTime());
		// groups[0] = c.getTime().getYear() + "년 " + c.getTime().getMonth() +
		// "월 " + c.getTime().getDate() + "일";
		// c.setTimeInMillis(date.getTime() + AlarmManager.INTERVAL_DAY);
		// groups[1] = c.getTime().getYear() + "년 " + c.getTime().getMonth() +
		// "월 " + c.getTime().getDate() + "일";
		// c.setTimeInMillis(date.getTime() + AlarmManager.INTERVAL_DAY * 2);
		// groups[2] = c.getTime().getYear() + "년 " + c.getTime().getMonth() +
		// "월 " + c.getTime().getDate() + "일";
		todayList = dao.getKmaBodyCursor(0);
		tomorrowList = dao.getKmaBodyCursor(1);
		afterTomorrowList = dao.getKmaBodyCursor(2);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		ArrayList<KmaInfoBean> list = null;
		switch (groupPosition) {
		case 0:
			list = todayList;
			break;
		case 1:
			list = tomorrowList;
			break;
		case 2:
			list = afterTomorrowList;
			break;
		}
		return list;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.weather_list_item, parent, false);
		}
		TextView timeTv = (TextView) convertView.findViewById(R.id.tvTime);
		TextView infoTv = (TextView) convertView.findViewById(R.id.tvInfo);
		ImageView infoIv = (ImageView) convertView.findViewById(R.id.imgInfo);
		switch (groupPosition) {
		case 0:
			timeTv.setText(todayList.get(childPosition).getTime() + "시");
			infoTv.setText(todayList.get(childPosition).getState() + "	" + todayList.get(childPosition).getTemp() + "℃	" + todayList.get(childPosition).getPop() + "％(강수확률)	"
					+ todayList.get(childPosition).getReh() + "％(습도)	" + todayList.get(childPosition).getWind_direction() + "풍	" + todayList.get(childPosition).getWind_speed() + "㎧");
			setKmaImg(infoIv, todayList.get(childPosition).getState());
			break;
		case 1:
			timeTv.setText(tomorrowList.get(childPosition).getTime() + "시");
			infoTv.setText(tomorrowList.get(childPosition).getState() + "	" + tomorrowList.get(childPosition).getTemp() + "℃	" + tomorrowList.get(childPosition).getPop() + "％(강수확률)	"
					+ tomorrowList.get(childPosition).getReh() + "％(습도)	" + tomorrowList.get(childPosition).getWind_direction() + "풍	" + tomorrowList.get(childPosition).getWind_speed() + "㎧");
			setKmaImg(infoIv, tomorrowList.get(childPosition).getState());
			break;
		case 2:
			timeTv.setText(afterTomorrowList.get(childPosition).getTime() + "시");
			infoTv.setText(afterTomorrowList.get(childPosition).getState() + "	" + afterTomorrowList.get(childPosition).getTemp() + "℃	" + afterTomorrowList.get(childPosition).getPop() + "％(강수확률)	"
					+ afterTomorrowList.get(childPosition).getReh() + "％(습도)	" + afterTomorrowList.get(childPosition).getWind_direction() + "풍	" + afterTomorrowList.get(childPosition).getWind_speed()
					+ "㎧");
			setKmaImg(infoIv, afterTomorrowList.get(childPosition).getState());
			break;

		default:
			break;
		}
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		int cnt = 0;
		switch (groupPosition) {
		case 0:
			cnt = todayList.size();
			break;
		case 1:
			cnt = tomorrowList.size();
			break;
		case 2:
			cnt = afterTomorrowList.size();
			break;
		}
		return cnt;
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return groups[groupPosition];
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return groups.length;
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		TextView textView = getGenericView();
		textView.setText(getGroup(groupPosition).toString());
		return textView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void onGroupExpanded(int groupPosition) {
		// TODO Auto-generated method stub
		super.onGroupExpanded(groupPosition);
		// Log.d(TAG, "onGroupExpanded: " + groupPosition);
		// if (groupPosition != lastExpandedGroupPosition) {
		// onGroupCollapsed(lastExpandedGroupPosition);
		// }
		// super.onGroupExpanded(groupPosition);
		// lastExpandedGroupPosition = groupPosition;
	}

	@Override
	public void onGroupCollapsed(int groupPosition) {
		// TODO Auto-generated method stub
		super.onGroupCollapsed(groupPosition);
		Log.d(TAG, "onGroupCollapsed: " + groupPosition);
	}

	public TextView getGenericView() {
		// Layout parameters for the ExpandableListView
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100);

		TextView textView = new TextView(mContext);
		textView.setLayoutParams(lp);
		// Center the text vertically
		textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		// Set the text starting position
		textView.setPadding(50, 0, 0, 0);
		return textView;
	}

	public void setKmaImg(ImageView iv, String state) {

		switch (WeahterState.toState(state)) {
		case 흐림:
			iv.setImageResource(R.drawable.wb_cloudy);
			break;

		case 맑음:
			iv.setImageResource(R.drawable.wb_clearly);
			break;
		case 구름조금:
			iv.setImageResource(R.drawable.wb_little_cloudy);
			break;
		case 구름많음:
			iv.setImageResource(R.drawable.wb_mostly_cloudy);
			break;
		case 비:
			iv.setImageResource(R.drawable.wb_rainy);
			break;
		case 눈:
			iv.setImageResource(R.drawable.wb_snow);
			break;
		case 눈비:
			iv.setImageResource(R.drawable.wb_snow_rain);
			break;

		default:
			iv.setImageResource(R.drawable.wb_snow_rain);
			break;
		}
	}

	enum WeahterState {
		흐림, 맑음, 구름조금, 구름많음, 비, 눈, 눈비;

		public static WeahterState toState(String str) {
			return valueOf(str.toUpperCase());
		}
	}
}
